package j2seperf.io;

import java.io.*;

public class SafeExtraFastCopy2 {

	static final int BUFF_SIZE = 100000;
	static final byte[] buffer = new byte[BUFF_SIZE];

	public static void copy(String from, String to) throws IOException {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(from);
			out = new FileOutputStream(to);
			while (true) {
				synchronized (buffer) {
					int amountRead = in.read(buffer);
					if (amountRead == -1) {
						break;
					}
					out.write(buffer, 0, amountRead);
				}
			}
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
	}

	public static void main(String[] args) {
		try {
			Stopwatch timer = new Stopwatch().start();
			for (int i = 0; i < 20; i++) {
				copy("o.dat", "output.dat");
			}
			timer.stop();
			System.out.println(timer.getElapsedTime() / 20);
		} catch (IOException e) {
			e.printStackTrace();
		}
		long mem = Runtime.getRuntime().totalMemory()
				- Runtime.getRuntime().freeMemory();
		System.out.println("Mem Consumed = " + mem / 1000 + " kb");
	}
}
